<?php
declare(strict_types=1);

namespace app\store\service;
use app\common\model\wxapp\Setting as WxappSettingModel;
use app\common\library\wechat\WxBase;

class Employee
{
    /**
     * 获取小程序码
     */
    public static function getCode($employeeId, $path)
    {
        // 获取当前小程序信息
        $wxConfig = self::getMpWxConfig();
        // 微信登录 (获取session_key)
        $WxUser = new WxBase($wxConfig['app_id'], $wxConfig['app_secret']);
        $scene = 'employeeId='.$employeeId;
        $buffer = $WxUser->getCode($scene, $path);
        return $buffer;
    }

    /**
     * 获取微信小程序配置项
     * @return array
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private static function getMpWxConfig(): array
    {
        $wxConfig = WxappSettingModel::getWxappConfig();
        if (empty($wxConfig['app_id']) || empty($wxConfig['app_secret'])) {
            throwError('请到后台小程序设置填写AppID和AppSecret参数');
        }
        return $wxConfig;
    }
}
